"""测评题目模型"""
# 字段类型引入 column列
from sqlalchemy import Column, Integer, String, DateTime, func, Text, ForeignKey, text
from soulapi.db import Base, engine

# relationship SQLAlchemy ORM提供的方法
from sqlalchemy.orm import relationship

# 基于BaseNoel定义请求体的结构（json对象）
class Test(Base):
    __tablename__ = 'test'  # 数据表表名，__tablename__：设置映射到的表名不设置，默认映射到类名post下
    __table_args__ = {'comment': '测评题目'}
    te_id = Column(Integer, primary_key=True, index=True, autoincrement=True, server_default=text("0"), comment='题目ID')  # index索引设置,autoincremen自增长
    te_title = Column(String(100), nullable=False, server_default=text("''"),  comment="题目标题")
    te_intro = Column(Text, nullable=False, comment="题目描述")
    te_content = Column(Text, nullable=False, comment="题目内容")
    # 外键
    ad_id = Column(Integer, ForeignKey('admin.ad_id'), comment="发布测评题目:管理员")
    tety_id = Column(Integer, ForeignKey('testtype.tety_id'), comment="测评题目类型")
    # 关系(back_populates双向显示)
    admin = relationship('Admin', back_populates='test', foreign_keys=[ad_id])
    testtype = relationship('TestType', back_populates='test', foreign_keys=[tety_id])
    # 时间获取
    created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')


# 在数据库中生成表
Base.metadata.create_all(engine)